<?php
class BrandAction extends Action
{
    public function index()
    {
        $FloorBrandModel = M('FloorBrand');
        $BrandModel = M('Brand');
        $BrandNewsModel = M('BrandNews');
        $brands = $BrandModel->field('id,name,ename,initials')->select();
        dump(count($brands));
        $map = array();
        echo '<table>';
        foreach ($brands as $key => $brand) {
            unset($map);
            //获取柜台数量
            $map['bid'] = $brand['id'];
            $fbCount = $FloorBrandModel->where($map)->count();
            //获取底图数量
            $psdCount = $this->getPsdCount($brand['initials'] . '-' . $brand['name']);
            //获取最后一次更新的活动信息时间
            $new = $BrandNewsModel->field('end_time')->where($map)->order('end_time')->find();
            $lastDate = 0;
            if ($new) {
                $lastDate = date('Y.m.d', $new['end_time']);
            }
            //$brands[$key]['fb_count'] = $fbCount;
            //$brands[$key]['psd_count'] = $psdCount;
            //$brands[$key]['last_date'] = $lastDate;
            echo '<tr>
            <td>' . $brand['id'] . '</td>
            <td>' . $brand['initials'] . '</td>
            <td>' . $brand['name'] . '</td>
            <td>' . $brand['ename'] . '</td>
            <td>' . $fbCount . '</td>
            <td>' . $psdCount . '</td>
            <td>' . $lastDate . '</td>
            </tr>';
        }
        echo '</table>';
    }

    /**
     * 获取小图地图数量
     * @param $name
     */
    private function getPsdCount($name)
    {
        $count = 0;
        $brandPath = 'D:/GIT/material/' . $name . '/news';
        if (IS_WIN) {
            $brandPath = auto_charset($brandPath, 'utf-8', 'gbk');
        }
        if (is_dir($brandPath)) {
            $handler = opendir($brandPath);
            while (($filename = readdir($handler)) !== false) {
                if ($filename != '.' && $filename != '..') {
                    $count++;
                }
            }
            closedir($handler);
        }
        return $count;
    }
}